package com.example;

import com.example.config.IgniteConfig;
import com.example.model.Device;
import com.example.model.DeviceType;
import com.example.service.DeviceService;
import com.example.service.Tenant;
import lombok.extern.slf4j.Slf4j;
import org.apache.ignite.Ignite;
import org.apache.ignite.Ignition;

import java.util.Date;

@Slf4j
public class IgniteApplication {
    public static void main(String[] args) {
        try {
            // Start Ignite node
            Ignite ignite = Ignition.start(IgniteConfig.getConfiguration());
            log.info("Ignite node started successfully");

            // Initialize services
            DeviceService deviceService = new DeviceService(ignite);

            // Test T1 tenant
            deviceService.setTenant(Tenant.T1);
            populateData(deviceService);
            testTenant(deviceService);

            // Test T2 tenant
            deviceService.setTenant(Tenant.T2);
            populateData2(deviceService);
            testTenant(deviceService);

        } catch (Exception e) {
            log.error("Error running Ignite application", e);
        }
    }

    private static void testTenant(DeviceService deviceService) {
        log.info("Testing tenant: {}", deviceService.getCurrentTenant());

        // Test device queries
        var recentDevices = deviceService.findRecentDevices();
        log.info("Found {} recent devices", recentDevices.size());
        recentDevices.forEach(
               device -> log.info(device.toString())
        );


        // Test device type queries
        var deviceTypes = deviceService.findAllDeviceTypes();
        log.info("Found {} device types", deviceTypes.size());
        deviceTypes.forEach(
                deviceType -> log.info(deviceType.toString())
        );
    }

    private static void populateData(DeviceService deviceService){
        DeviceType type=new DeviceType(1L,"E16","Linde E16",Boolean.TRUE);
        deviceService.saveDeviceType(type);
        deviceService.saveDevice(new Device(1L,"001",type.getId(),"苏A0001",true,new Date()));
        deviceService.saveDevice(new Device(2L,"002",type.getId(),"苏A0002",true,new Date()));
        deviceService.saveDevice(new Device(3L,"003",type.getId(),"苏A0003",true,new Date()));

        DeviceType type2 = new DeviceType(2L,"CPCD50","HC CPCD50",Boolean.TRUE);
        deviceService.saveDeviceType(type2);
        deviceService.saveDevice(new Device(101L,"0X1",type2.getId(),"沪A0001",true,new Date()));
        deviceService.saveDevice(new Device(102L,"0X2",type2.getId(),"沪A0002",true,new Date()));
        deviceService.saveDevice(new Device(103L,"0X3",type2.getId(),"沪A0003",true,new Date()));
    }

    private static void populateData2(DeviceService deviceService){
        DeviceType type=new DeviceType(1L,"CPD35","BYD CPD35",Boolean.TRUE);
        deviceService.saveDeviceType(type);
        deviceService.saveDevice(new Device(1L,"001",type.getId(),"苏B0011",true,new Date()));
        deviceService.saveDevice(new Device(2L,"002",type.getId(),"苏B0012",true,new Date()));
        deviceService.saveDevice(new Device(3L,"003",type.getId(),"苏B0013",true,new Date()));

        DeviceType type2 = new DeviceType(2L,"CPCD50","HC CPCD50",Boolean.TRUE);
        deviceService.saveDeviceType(type2);
        deviceService.saveDevice(new Device(101L,"0X1",type2.getId(),"沪B1001",true,new Date()));
        deviceService.saveDevice(new Device(102L,"0X2",type2.getId(),"沪B1002",true,new Date()));
        deviceService.saveDevice(new Device(103L,"0X3",type2.getId(),"沪B1003",true,new Date()));
    }

}
